package LeetCode.LC130;

/**
 * @author corn
 * @create 2020-08-03-21:45
 */

/**
 * 130. 被围绕的区域
 * 给定一个二维的矩阵，包含 'X' 和 'O'（字母 O）。
 * 找到所有被 'X' 围绕的区域，并将这些区域里所有的 'O' 用 'X' 填充。
 */
public class Solve {
    public static void main(String[] args) {
        char[][] board ={{'O','O','O'},{'O','O','O'},{'O','O','O'}};
        Solution solution = new Solution();
        for(int i = 0; i < board.length ; i++) {
            for (int j = 0; j < board[0].length; j++) {
                System.out.print(board[i][j]);
            }
            System.out.println();
        }
        solution.solve(board);
    }
}
class Solution {
    public void solve(char[][] board) {
        if(board == null || board.length == 0 || board[0].length == 0){
            return;
        }
        int col = board.length;
        int var = board[0].length;
        for(int i = 0; i < col ; i++){
            for (int j = 0; j < var; j++) {
                if(board[i][j] == 'O'){
                    if(i == 0 || j == 0 || i == col-1 || j == var-1){
                        change(board,i,j,col,var);
                    }
                }
            }
        }

        for(int i = 0; i < col ; i++){
            for (int j = 0; j < var; j++) {
                if(board[i][j] == 'O'){
                    board[i][j] = 'X';
                }
                if(board[i][j] == '1'){
                    board[i][j] = 'O';
                }
            }
        }

    }
    public void change(char[][] board,int i,int j, int col,int var){
        if(i < 0 || j < 0 || i == col || j == var){
            return;
        }
        if( board[i][j] == 'O'){
            board[i][j] = '1';
        }else{
            return;
        }
        //向下
        change(board,i+1,j,col,var);
        //向上
        change(board,i-1,j,col,var);
        //向右
        change(board,i,j+1,col,var);
        //向左
        change(board,i,j-1,col,var);
        return;
    }
}
